# Method to return info about the versions of Rails installed and paths
# of the dummy apps and this library.
module TestEngine::SystemHelper

  # Will return the desired rails version or latest version.
  def rails_version
    ENV['RAILS_VERSION'] || clean_sh('rails -v').split(' ').last
  end

  # Will return a version of Rails >= 3.0 that is installed on this system
  # We MUST shell out rather than just query the API so we can determine this
  # without the influence of Bundler.
  def older_installed_rails
    clean_sh(%q{ruby -e "
        require 'rubygems';
        puts Gem::Specification.find_all_by_name('rails').
          collect(&:version).reject {|v| v < Gem::Version.create(3)}[-2]
      "}).chop
  end

  # We need our own rails stub since the one generated by Rubygems now longer
  # works to generate old apps.
  def rails_stub
    library_path.join 'share/rails'
  end

  # Where the app template is located for tweaking the newly generated app.
  def template_path
    library_path.join 'share/template.rb'
  end

  # The root path of this library
  def library_path
    Pathname.new(__FILE__).dirname.join '../../'
  end

  # The Gemfile for a specific app (latest rails version by default)
  def gemfile_path version=rails_version
    app_path(version).join 'Gemfile'
  end

  # A specific app (latest rails version by default)
  def app_path version=rails_version
    dummy_apps_path.join "v#{version}"
  end

  # Assumes we are running in the context of the Rakefile in the engine
  # being tested therefore PWD is the project root.
  def dummy_apps_path
    Pathname.new "tmp/dummy_apps"
  end

end